var e = getApp(),
    a = require("../../utils/config");

Page({
  data: {
    videoad: "adunit-2c444468a3401bb5"
  },
  onLoad: function () {
    e.setTitle(), e.setNavigationBar(), this.getIndexData();
    var a = this;
    "" != tt.getStorageSync("user_id") ? a.setData({
      is_login: 1
    }) : a.setData({
      is_login: 0
    });
  },
  onShow: function () {
    console.log("user_id" + tt.getStorageSync("user_id"));
    var e = this,
        a = tt.getStorageSync("wxapp").videoad;
    a && e.setData({}), "" != tt.getStorageSync("user_id") ? e.setData({
      is_login: 1
    }) : e.setData({
      is_login: 0
    });
  },
  uploadImage: function (e, a) {
    var t = this;
    tt.chooseImage({
      count: 1,
      sizeType: ["original", "compressed"],
      sourceType: [a],
      success: function (a) {
        var i = a.tempFilePaths[0];
        tt.showLoading({
          title: "图片上传中..."
        }), tt.uploadFile({
          url: "https://imgapi.faxingwu.com/api/upload",
          filePath: i,
          name: "image",
          formData: {
            image: i,
            type: 1,
            proname: "manhua"
          },
          header: {
            "Content-Type": "application/x-www-form-urlencoded"
          },
          success: function (a) {
            tt.hideLoading();
            var i = a.data,
                o = JSON.parse(i);

            if (101 == o.code) {
              var n = o.data[0];
              console.log(n), tt.showLoading({
                title: "图片检测中..."
              }), tt.request({
                url: "https://dou.j391.com/dongman/img_sec_check.php",
                data: {
                  p: "http://image.faxingwu.com" + n,
                  appid: getApp().globalData.appid
                },
                method: "POST",
                header: {
                  "Content-Type": "application/x-www-form-urlencoded"
                },
                success: function (a) {
                  let data = {};
                  data.errcode = 0;
                  a.data = data;
                  console.log("e.errcode=" + a.data.errcode), tt.hideLoading(), 0 == a.data.errcode ? (tt.setStorageSync("userImage", n), t.makeInfo(n, e)) : tt.showToast({
                    title: "内容含有违法违规内容",
                    icon: "none",
                    duration: 1e3
                  });
                }
              });
            } else tt.showToast({
              title: "图片上传失败",
              icon: "none",
              duration: 1e3
            });
          }
        });
      }
    });
  },
  makeInfo: function (e, t) {
    if ("" == (i = tt.getStorageSync("uid"))) var i = 0;
    tt.showLoading({
      title: "图片绘制中..."
    }), tt.request({
      url: a.API_HOST + "/huihua/result/new-create-image",
      data: {
        uid: i,
        image: e,
        image_id: t
      },
      method: "POST",
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (e) {
        if (console.log(e.data), 101 == e.data.code) {
          var a = e.data.result_img;
          tt.hideLoading(), tt.navigateTo({
            url: "/pages/making/making?result_image=" + a
          });
        } else tt.hideLoading(), tt.showToast({
          title: "图片识别失败,请重新上传",
          icon: "none",
          duration: 1e3
        });
      }
    });
  },
  openCamera: function () {
    tt.setStorage({
      key: "image_id",
      data: 8
    }), this.uploadImage(8, "camera");
  },
  openAlbum: function () {
    tt.setStorage({
      key: "image_id",
      data: 8
    }), this.uploadImage(8, "album");
  },
  upload: function () {
    tt.setStorage({
      key: "image_id",
      data: 8
    }), this.uploadImage(8);
  },
  getUserInfo: function (e) {
    tt.navigateTo({
      url: "../login/login"
    });
  },
  getIndexData: function () {
    var a = this;

    e._get("index/page", {}, function (e) {
      a.setData(e.data);
    });
  },
  imagesHeight: function (e) {
    var a = e.target.dataset.id,
        t = e.target.dataset.itemKey,
        i = 750 / (e.detail.width / e.detail.height),
        o = this.data.imgHeights;
    void 0 === o[t] && (o[t] = {}), o[t][a] = i;
    var n = this.data.imgCurrent;
    void 0 === n[t] && (n[t] = Object.keys(this.data.items[t].data)[0]), this.setData({
      imgHeights: o,
      imgCurrent: n
    });
  },
  bindChange: function (e) {
    var a = e.target.dataset.itemKey,
        t = this.data.imgCurrent;
    t[a] = e.detail.currentItemId, this.setData({
      imgCurrent: t
    });
  },
  goTop: function (e) {
    this.setData({
      scrollTop: 0
    });
  },
  scroll: function (e) {
    this.setData({
      indexSearch: e.detail.scrollTop
    }), e.detail.scrollTop > 300 ? this.setData({
      floorstatus: !0
    }) : this.setData({
      floorstatus: !1
    });
  },
  onShareAppMessage: function () {
    return {
      title: "动漫人像生成器",
      path: "/pages/index/index",
      imageUrl: tt.getStorageSync("wxapp").navbar.imageUrl
    };
  }
});